package com.whirl.basic.service.impl;

import com.whirl.basic.mapper.BaseMapper;
import com.whirl.basic.query.BaseQuery;
import com.whirl.basic.service.BaseService;
import com.whirl.basic.utils.PageList;
import com.whirl.org.entity.Employee;
import com.whirl.org.mapper.EmployeeMapper;
import com.whirl.org.query.EmployeeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaowei
 * @date 2022/6/27 14:02
 */

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T selectById(Long id) {
        return (T)baseMapper.selectById(id);
    }

    @Override
    public List<T> selectAll() {
        return baseMapper.selectAll();
    }

    /**
     * 分页查询
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        query.setCurrentPage(query.getStart());
        System.out.println("query参数"+query);
        //1.查询总条数
        Long total = baseMapper.queryCount(query);
        //2.查询当前的页数
        if(total>0L){
            List<T> rows = baseMapper.queryData(query);
            return new PageList<>(total,rows);
        }
        else {
            return new PageList<>();
        }
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}
